
/**
 * 初始化日期时间控件
 */
function initDateTimeControls() {
	// Error发生时间
	new Rolldate({
		el: '#errorDate',
		format: 'YYYY-MM-DD',
		beginYear: 2015,
		endYear: 2050,
		trigger: 'click',
		lang: {
			title: '请选择发生日期',
		},
		confirm: function(date) {
			
		}
	})
	
	//跟踪日期
	new Rolldate({
		el: '#trackDate',
		format: 'YYYY-MM-DD',
		beginYear: 2015,
		endYear: 2050,
		trigger: 'click',
		lang: {
			title: '请选择跟踪日期',
		},
		confirm: function(date) {
			
		}
	})
	
	
}

/**
 * 参数校验
 */
function parameterVerification() {
	
	// Error发生时间
	var errorDate = h('#errorDate').html()
	if (!errorDate || errorDate == '请选择时间') {
		app.closeWaiting()
		app.toast('请选择Error发生时间')
		return false
	}
	// 客户英文单位名称
	var enCompanyName = h('#enCompanyName').val()
	if (!enCompanyName) {
		app.closeWaiting()
		app.toast('请输入客户英文单位名称')
		return false
	}
	// 问题描述
	var problemStatement = h('#problemStatement').val()
	if (!problemStatement) {
		app.closeWaiting()
		app.toast('请输入问题描述')
		return false
	}
	// 原因
	var rootCause = h('#rootCause').val()
	if (!rootCause) {
		app.closeWaiting()
		app.toast('请输入原因')
		return false
	}
	// 补救措施
	var correctiveActionTaken = h('#correctiveActionTaken').val()
	if (!correctiveActionTaken) {
		app.closeWaiting()
		app.toast('请输入补救措施')
		return false
	}
	// 跟踪时间
	var trackDate = h('#trackDate').html()
	if (!trackDate || trackDate == '请选择时间') {
		app.closeWaiting()
		app.toast('请选择跟踪时间')
		return false
	}
	
	if (stringYYYYMMDDToDate(errorDate).getTime() > stringYYYYMMDDToDate(trackDate).getTime()) {
		app.toast('Error发生时间不能大于跟踪时间')
		return false
	}
	
	// parentCodeValue
	var parentCode = h('#parentCode').html()
	if (!parentCode || parentCode == '请选择') {
		app.closeWaiting()
		app.toast('请选择Error代码类别')
		return false
	}
	
	//error 代码
	var errCode = h("#errCode").html()
	if(!errCode || errCode=="请选择"){
		app.closeWaiting()
		app.toast("请填写Error代码");
		return false
	}
	return true
}

/*'yyyy-MM-dd HH:mm'格式的字符串转日期*/
function stringYYYYMMDDToDate(str) {
	var tempStrs = str.split(" ");
	var dateStrs = tempStrs[0].split("-");
	var year = parseInt(dateStrs[0], 10);
	var month = parseInt(dateStrs[1], 10) - 1;
	var day = parseInt(dateStrs[2], 10);
	var date = new Date(year, month, day);
	return date;
}

/**
 * 新增Error Report
 */
function addSaveErrorData(tips, callBack) {
	if (!parameterVerification()) {
		h('#saveBtn').removeAttr('disabled');
		h('#submitBtn').removeAttr('disabled');
		return
	}
	var enginnerId = app.getUser().enginnerId
	var fseName = app.getUser().name;
	var params = {
		serviceNo: h('#serviceNo').html(),
		errorDate: h('#errorDate').html(),
		enCompanyName: h('#enCompanyName').val(),
		problemStatement: h('#problemStatement').val(),
		rootCause: h('#rootCause').val(),
		correctiveActionTaken: h('#correctiveActionTaken').val(),
		trackDate: h('#trackDate').html(),
		enginnerId: enginnerId,
		fseName: fseName,
		planId : h('#planId').val(),
		noticeId : h('#noticeId').val(),
		takenBy : enginnerId,
		parentValue:  h('#parentCode').html(),
		parentCode:  h('#parentCodeValue').val(),
		errValue: h('#errCode').html(),
		errCode: h('#errCodeValue').val(),
		errType: h('#errType').val(),
		errLongdesc : h('#errLongdesc').html(),
		errResponman : h('#errResponman').val(),
		helpId: h('#helpId').val()
	}
	if(typeof(params) == "object"){
		var code = String.fromCharCode(8198)
		Object.keys(params).forEach(function (key) {
			if (typeof(params[key]) == 'string'){
				var str = params[key].split(code).join(' ')
				params[key]=str
				str = null;
			}
			// params[key].split(code).join('!!!!!')

		})
		console.debug(params)
	}
	if (!app.getNetworkState()) {
		h('#saveBtn').removeAttr('disabled');
		h('#submitBtn').removeAttr('disabled');
		app.closeWaiting()
		app.toast("无网络")
		return
	}
	app.showWaiting(tips)
	ajaxPost(BASE_URL + 'cc/error/report/addErrorReport', params, function(data) {
		app.closeWaiting()
		callBack(data)
	}, function(err) {
		h('#saveBtn').removeAttr('disabled');
		h('#submitBtn').removeAttr('disabled');
		app.closeWaiting()
		app.toast(err)
	})
}



function updateErrorData(tips, callBack) {
	if (!parameterVerification()) {
		return
	}

	var enginnerId = app.getUser().enginnerId
	var fseName = app.getUser().name;
	var params = {
		id: h('#id').val(),
		serviceNo: h('#serviceNo').html(),
		errorDate: h('#errorDate').html(),
		enCompanyName: h('#enCompanyName').val(),
		problemStatement: h('#problemStatement').val(),
		rootCause: h('#rootCause').val(),
		correctiveActionTaken: h('#correctiveActionTaken').val(),
		trackDate: h('#trackDate').html(),
		enginnerId: enginnerId,
		fseName: fseName,
		planId : h('#planId').val(),
		noticeId : h('#noticeId').val(),
		takenBy : enginnerId,
		parentValue: h('#parentCode').html(),
		parentCode: h('#parentCodeValue').val(),
		errValue: h('#errCode').html(),
		errCode: h('#errCodeValue').val(),
		errType: h('#errType').val(),
		errLongdesc : h('#errLongdesc').html(),
		errResponman : h('#errResponman').val(),
		helpId:h('#helpId').val(),
	}
	if (!app.getNetworkState()) {
		app.closeWaiting()
		app.toast("无网络")
		return
	}
	app.showWaiting(tips)
	ajaxPost(BASE_URL + 'cc/error/report/updateErrorReport', params, function(data) {
		callBack(data)
	}, function(err) {
		app.closeWaiting()
		app.toast(err)
	})
}

function errorSubmit(){
	ajaxGet(BASE_URL + 'cc/error/report/errorReportSubmit', {
		id: id,
		enginnerId: app.getUser().enginnerId
	}, function(data) {
		app.closeWaiting();
		if (data.code === 200) {
			app.toast("提交成功")
			// 查找需要跳转的页面，并指定刷新的方法
			var erroOrHot = plus.webview.getWebviewById('view/service/report/errorhot/erroOrHot.html');
			if (erroOrHot) {
				erroOrHot.close()
			}
			mui.back();
		} else {
			h('#saveBtn').removeAttr('disabled');
			h('#submitBtn').removeAttr('disabled');
			app.toast(data.msg)
		}
	}, function(errorMsg) {
		app.closeWaiting()
		h('#saveBtn').removeAttr('disabled');
		h('#submitBtn').removeAttr('disabled');
		app.toast(errorMsg)
	});
}